package logic

import (
	"book-hub/bookrpc/bookrpc"
	"book-hub/bookrpc/internal/svc"
	"book-hub/models"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectAddLogic {
	return &CollectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectAddLogic) CollectAdd(in *bookrpc.CollectAddRequest) (*bookrpc.CollectAddResponse, error) {
	// todo: add your logic here and delete this line
	var novel models.Novel
	models.DB.Where("id=?", in.NovelId).Find(&novel)
	if novel.ID == 0 {
		return nil, fmt.Errorf("当前小说不存在")
	}
	var collect models.Collect
	models.DB.Where("Novel_id=?", in.NovelId).Find(&collect)
	if collect.ID != 0 {
		return nil, fmt.Errorf("当前小说以收藏")
	}
	collect = models.Collect{
		NovelId: int(in.NovelId),
		UserId:  int(in.UserId),
	}
	models.DB.Create(&collect)
	novel.CollectCount = novel.CollectCount + 1
	models.DB.Updates(&novel)
	return &bookrpc.CollectAddResponse{
		CollectId: int64(collect.ID),
	}, nil
}
